<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addClickRole(1, '0')">添加权限</el-button>
        </div>
        <el-table :data="list" border row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- 参数1是2 表示二级  参数2 一级权限的id
                row.id  爸爸的id = 自己的pid
               -->
              <el-button v-if="row.type === 1" size="small" type="text" @click="addClickRole(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="showEditRole(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="title" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form v-loading="loading" label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
              active-text="启用"
              inactive-text="未启用"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { transformDepartments } from '@/utils/index'
import {
  reqGetPermissionList,
  reqDelPermission,
  reqAddPermission,
  reqGetPermissionDetail,
  reqUpdatePermission } from '@/api/permission'
export default {
  name: 'Permission',
  data() {
    return {
      list: [], // 表格数据
      showDialog: false, // 控制对话框展示的布尔值
      formData: {
        enVisible: '0', // 1 表示开启  0表示不开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      loading: false,
      title: ''
    }
  },
  computed: {
    // title() {
    //   return this.formData.id ? '编辑权限' : '添加权限'
    // }
  },
  created() {
    // 获取所有权限列表
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      this.list = transformDepartments(data, '0')
    },
    // 删除权限点
    delRole(id) {
      // 确认消息框
      this.$confirm('确认删除该权限点么？操作不可逆', '温馨提示').then(async() => {
        // 发送请求
        const { message } = await reqDelPermission(id)
        // 提示用户
        this.$message.success(message)
        // 重新渲染
        this.getPermissionList()
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    // 添加权限点显示对话框
    addClickRole(type, pid) {
      this.title = '添加权限'
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 关闭对话框
    closeDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 1 表示开启  0表示不开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 添加/更新权限点完成
    async clickSubmit() {
      if (this.formData.id) {
        // 编辑提交
        const { message } = await reqUpdatePermission(this.formData)
        this.$message.success(message)
      } else {
        // 添加
        if (this.formData.name.trim().length === 0 || this.formData.code.trim().length === 0) {
          return this.$message.error('请输入权限名或权限编码')
        }
        // 提示用户
        const { message } = await reqAddPermission(this.formData)
        this.$message.success(message)
      }
      // 关闭对话框
      this.closeDialog()
      // 重新渲染
      this.getPermissionList()
    },
    // 点击查看渲染对话框
    async showEditRole(id) {
      this.title = '编辑权限'
      this.loading = true
      this.showDialog = true
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
      this.loading = false
    }
  }
}
</script>
